import Banner from "../Banner";
import { ItemData, PlayerData } from "../Datas/Data";
import { Constant, ItemType } from "../Framework/Const/Constant";
import AudioManager from "../Framework/Managers/AudioManager";
import { DataManager } from "../Framework/Managers/DataManager";
import { EventManager, MyEvent } from "../Framework/Managers/EventManager";
import { PoolManager } from "../Framework/Managers/PoolManager";
import { UIManager } from "../Framework/Managers/UIManager";
import NodeUtil from "../Framework/Utils/NodeUtil";
import GoodsItem from "./GoodsItem";
import InventoryButton from "./InventoryButton";

const { ccclass, property } = cc._decorator;

export enum InventoryType {
    None = "None",
    Helmet = "头盔",
    Bulletproof = "防弹衣",
    Weapon_0 = "主武器",
    Weapon_1 = "副武器",
    Pistol = "手枪",
    MeleeWeapon = "近战武器",
}

@ccclass
export default class InventoryPanel extends cc.Component {

    static Instance: InventoryPanel = null;

    Helmet: InventoryButton = null;
    Bulletproof: InventoryButton = null;
    Weapon_0: InventoryButton = null;
    Weapon_1: InventoryButton = null;
    Pistol: InventoryButton = null;
    MeleeWeapon: InventoryButton = null;

    buttons: InventoryButton[] = [];

    MoneyLabel: cc.Label = null;

    GoodsTitleLabel: cc.Label = null;
    GoodsContentLabel: cc.Label = null;
    GoodsScrollViewContent: cc.Node = null;
    PlayerGoodsContent: cc.Node = null;
    DetailPanel: cc.Node = null;
    DetailEquipButton: cc.Node = null;
    DetailGoodsContent: cc.Node = null;
    DetailGoodsLabel: cc.Label = null;
    DetailPutButton: cc.Node = null;
    DetailBuyBulletButton: cc.Node = null;

    PlayerGoodsItems: GoodsItem[] = [];
    GoodsItems: GoodsItem[] = [];
    DetailGoodsItem: GoodsItem = null;

    ItemDatas: ItemData[] = [];
    SelectItemData: ItemData = null;

    protected onLoad(): void {
        InventoryPanel.Instance = this;

        this.Helmet = NodeUtil.GetComponent("Helmet", this.node, InventoryButton);
        this.Bulletproof = NodeUtil.GetComponent("Bulletproof", this.node, InventoryButton);
        this.Weapon_0 = NodeUtil.GetComponent("Weapon_0", this.node, InventoryButton);
        this.Weapon_1 = NodeUtil.GetComponent("Weapon_1", this.node, InventoryButton);
        this.Pistol = NodeUtil.GetComponent("Pistol", this.node, InventoryButton);
        this.MeleeWeapon = NodeUtil.GetComponent("MeleeWeapon", this.node, InventoryButton);
        this.MoneyLabel = NodeUtil.GetComponent("MoneyLabel", this.node, cc.Label);

        this.buttons.push(this.Helmet);
        this.buttons.push(this.Bulletproof);
        this.buttons.push(this.Weapon_0);
        this.buttons.push(this.Weapon_1);
        this.buttons.push(this.Pistol);
        this.buttons.push(this.MeleeWeapon);

        this.GoodsTitleLabel = NodeUtil.GetComponent("GoodsTitleLabel", this.node, cc.Label);
        this.GoodsContentLabel = NodeUtil.GetComponent("GoodsContentLabel", this.node, cc.Label);
        this.GoodsScrollViewContent = NodeUtil.GetNode("GoodsScrollViewContent", this.node);
        this.DetailPanel = NodeUtil.GetNode("DetailPanel", this.node);
        this.DetailEquipButton = NodeUtil.GetNode("DetailEquipButton", this.node);
        this.DetailGoodsContent = NodeUtil.GetNode("DetailGoodsContent", this.node);
        this.DetailGoodsLabel = NodeUtil.GetComponent("DetailGoodsLabel", this.node, cc.Label);
        this.DetailPutButton = NodeUtil.GetNode("DetailPutButton", this.node);
        this.DetailBuyBulletButton = NodeUtil.GetNode("DetailBuyBulletButton", this.node);
        this.PlayerGoodsContent = NodeUtil.GetNode("PlayerGoodsContent", this.node);
    }

    Show(title: string, itemDatas: ItemData[]) {
        this.Helmet.Set(InventoryType.Helmet);
        this.Bulletproof.Set(InventoryType.Bulletproof);
        this.Weapon_0.Set(InventoryType.Weapon_0);
        this.Weapon_1.Set(InventoryType.Weapon_1);
        this.Pistol.Set(InventoryType.Pistol);
        this.MeleeWeapon.Set(InventoryType.MeleeWeapon);

        this.SelectItemData = null;
        this.ItemDatas = itemDatas;
        this.GoodsTitleLabel.string = `${title}`;
        this.RefreshGoodsItem();
        this.RefreshMoney();
        this.RefreshInventoryButtons();
        this.RefreshPlayerGoodsItem();
    }

    ShowDetailPanel(active: boolean, isEquippedItem: boolean = false) {
        this.DetailPanel.active = active;

        if (active) {
            if (this.DetailGoodsItem) PoolManager.PutNode(this.DetailGoodsItem.node);
            PoolManager.GetNode("Prefabs/UI/GoodsItem", this.DetailGoodsContent).then((node) => {
                let goods = node.getComponent(GoodsItem);
                goods.InitSimple(this.SelectItemData);
                this.DetailGoodsItem = goods;
                this.DetailGoodsLabel.string = `${this.SelectItemData.Name}`;
            });

            let inPlayerGoods = DataManager.PlayerData.BackpackItems.indexOf(this.SelectItemData) != -1;
            this.DetailPutButton.active = isEquippedItem || inPlayerGoods;
            this.DetailBuyBulletButton.active = isEquippedItem && ItemData.IsGun(ItemType[this.SelectItemData.Type]);
        } else {
            this.SelectItemData = null;
        }
    }

    RefreshGoodsItem() {
        this.GoodsItems.forEach((e) => PoolManager.PutNode(e.node));
        for (let i = 0; i < this.ItemDatas.length; i++) {
            const data = this.ItemDatas[i];
            PoolManager.GetNode("Prefabs/UI/GoodsItem", this.GoodsScrollViewContent).then((node) => {
                let goods = node.getComponent(GoodsItem);
                goods.Init(data, this.OnGoodsItemCallback.bind(this));
                this.GoodsItems.push(goods);
            });
        }
    }

    RefreshInventoryButtons() {
        this.buttons.forEach(e => e.Refresh());
    }

    RefreshPlayerGoodsItem() {
        this.PlayerGoodsItems.forEach((e) => PoolManager.PutNode(e.node));
        for (let i = 0; i < PlayerData.MaxBackpackItems; i++) {
            const data = DataManager.PlayerData.BackpackItems[i];
            PoolManager.GetNode("Prefabs/UI/GoodsItem", this.PlayerGoodsContent).then((node) => {
                let goods = node.getComponent(GoodsItem);
                if (data) {
                    goods.Init(data, this.OnGoodsItemCallback.bind(this));
                } else {
                    goods.InitBlank();
                }
                this.PlayerGoodsItems.push(goods);
            });
        }
    }

    OnGoodsItemCallback(data: ItemData) {
        this.GoodsItems.forEach((e) => e.SetSelected(data));
        this.PlayerGoodsItems.forEach((e) => e.SetSelected(data));

        this.SelectItemData = data;
        this.ShowDetailPanel(true);
    }

    InventoryType: InventoryType;

    OnButtonClick(event: cc.Event) {
        AudioManager.PlayAudio(Constant.Audio.ButtonClick);
        this.buttons.forEach(e => e.RefreshSelected(InventoryType[event.target.name]));

        switch (event.target.name) {
            case "Helmet"://头盔
                if (!DataManager.PlayerData.Helmet) {
                    UIManager.Instance.ShowPanel(Constant.Panel.ShopPanel, [ItemType.头盔]);
                } else {
                    this.SelectItemData = DataManager.PlayerData.Helmet;
                    this.ShowDetailPanel(true, true);
                }
                break;
            case "Bulletproof"://防弹衣
                if (!DataManager.PlayerData.Bulletproof) {
                    UIManager.Instance.ShowPanel(Constant.Panel.ShopPanel, [ItemType.防弹衣]);
                } else {
                    this.SelectItemData = DataManager.PlayerData.Bulletproof;
                    this.ShowDetailPanel(true, true);
                }
                break;
            case "Weapon_0"://主武器
                if (!DataManager.PlayerData.Weapon_0) {
                    UIManager.Instance.ShowPanel(Constant.Panel.ShopPanel, [ItemType.突击步枪]);
                } else {
                    this.SelectItemData = DataManager.PlayerData.Weapon_0;
                    this.ShowDetailPanel(true, true);
                }
                break;
            case "Weapon_1"://副武器
                if (!DataManager.PlayerData.Weapon_1) {
                    UIManager.Instance.ShowPanel(Constant.Panel.ShopPanel, [ItemType.射手步枪]);
                } else {
                    this.SelectItemData = DataManager.PlayerData.Weapon_1;
                    this.ShowDetailPanel(true, true);
                }
                break;
            case "Pistol"://手枪
                if (!DataManager.PlayerData.Pistol) {
                    UIManager.Instance.ShowPanel(Constant.Panel.ShopPanel, [ItemType.手枪]);
                } else {
                    this.SelectItemData = DataManager.PlayerData.Pistol;
                    this.ShowDetailPanel(true, true);
                }
                break;
            case "MeleeWeapon"://近战武器
                this.SelectItemData = DataManager.PlayerData.MeleeWeapon;
                this.ShowDetailPanel(true, true);
                break;
            case "ReturnButton":
                UIManager.Instance.HidePanel(Constant.Panel.InventoryPanel);
                break;
            case "DetailEquipButton":
                if (ItemData.IsMainGun(ItemType[this.SelectItemData.Type])) {
                    //装备 0 或者 1
                    if (DataManager.AddEquippedItem(InventoryType.Weapon_0, this.SelectItemData) || DataManager.AddEquippedItem(InventoryType.Weapon_1, this.SelectItemData)) {
                        this.RefreshGoodsItem();
                        AudioManager.PlayAudio(Constant.Audio.Equip);
                    } else {
                        UIManager.ShowTip("装备已满");
                    }
                }
                if (ItemType[this.SelectItemData.Type] == ItemType.手枪) {
                    if (DataManager.AddEquippedItem(InventoryType.Pistol, this.SelectItemData)) {
                        this.RefreshGoodsItem();
                        AudioManager.PlayAudio(Constant.Audio.Equip);
                    } else {
                        UIManager.ShowTip("已有装备");
                    }
                }
                if (ItemType[this.SelectItemData.Type] == ItemType.头盔) {
                    if (DataManager.AddEquippedItem(InventoryType.Helmet, this.SelectItemData)) {
                        this.RefreshGoodsItem();
                        AudioManager.PlayAudio(Constant.Audio.Equip);
                    } else {
                        UIManager.ShowTip("已有装备");
                    }
                }
                if (ItemType[this.SelectItemData.Type] == ItemType.防弹衣) {
                    if (DataManager.AddEquippedItem(InventoryType.Bulletproof, this.SelectItemData)) {
                        this.RefreshGoodsItem();
                        AudioManager.PlayAudio(Constant.Audio.Equip);
                    } else {
                        UIManager.ShowTip("已有装备");
                    }
                }
                if (ItemType[this.SelectItemData.Type] == ItemType.子弹 || ItemType[this.SelectItemData.Type] == ItemType.药品 || ItemType[this.SelectItemData.Type] == ItemType.战利品) {
                    if (DataManager.AddItemToBackpack(this.SelectItemData)) {
                        DataManager.RemovePlayerItem(this.SelectItemData);
                        this.RefreshGoodsItem();
                        AudioManager.PlayAudio(Constant.Audio.Equip);
                    }
                    else {
                        UIManager.ShowTip("背包已满");
                    }
                }

                this.ShowDetailPanel(false);
                break;
            case "DetailDetailButton":
                UIManager.Instance.ShowPanel(Constant.Panel.ItemDetailPanel, [this.SelectItemData]);
                break;
            case "DetailPutButton"://放回仓库
                if (this.SelectItemData.Type == ItemType[ItemType.近战武器]) return;
                if (DataManager.RemovePlayerEquippedItem(this.SelectItemData) || DataManager.RemoveItemFromBackpack(this.SelectItemData)) {
                    DataManager.AddPlayerItem(this.SelectItemData);
                    AudioManager.PlayAudio(Constant.Audio.Unequip);
                }
                break;
            case "DetailBuyBulletButton"://购买子弹
                UIManager.Instance.ShowPanel(Constant.Panel.ShopPanel, [ItemType.子弹]);
                break;
            case "DetailSellButton":
                if (this.SelectItemData) {
                    if (this.SelectItemData.Type == ItemType[ItemType.近战武器]) return;
                    let type = ItemType[`${this.SelectItemData.Type}`];
                    if (ItemData.IsEquip(type)) {
                        DataManager.Money += Math.ceil(this.SelectItemData.Price * (this.SelectItemData as any).Durability / 100);
                    } else {
                        DataManager.Money += this.SelectItemData.Price;
                    }
                    AudioManager.PlayAudio(Constant.Audio.GetMoney);
                    DataManager.RemovePlayerItem(this.SelectItemData);
                    DataManager.RemovePlayerEquippedItem(this.SelectItemData);
                    DataManager.RemoveItemFromBackpack(this.SelectItemData);
                    this.RefreshGoodsItem();
                    this.ShowDetailPanel(false);
                }
                break;
            case "DetailCloseButton":
                this.ShowDetailPanel(false);
                break;
            case "AddMoneyButton":
                Banner.Instance.ShowVideoAd(() => {
                    AudioManager.PlayAudio(Constant.Audio.GetMoney);
                    DataManager.Money += 20000;
                });
                break;
        }

        EventManager.Scene.emit(MyEvent.RefreshEquip);
        this.RefreshInventoryButtons();
    }

    RefreshMoney() {
        this.MoneyLabel.string = `${DataManager.Money}`;
    }

    protected onEnable(): void {
        EventManager.on(MyEvent.RefreshMoney, this.RefreshMoney, this);
        EventManager.on(MyEvent.RefreshGoodsItem, this.RefreshGoodsItem, this);
        EventManager.on(MyEvent.RefreshBackpackGoodsItem, this.RefreshPlayerGoodsItem, this);
        EventManager.on(MyEvent.RefreshInventoryButtons, this.RefreshInventoryButtons, this);
    }

    protected onDisable(): void {
        EventManager.off(MyEvent.RefreshMoney, this.RefreshMoney, this);
        EventManager.off(MyEvent.RefreshGoodsItem, this.RefreshGoodsItem, this);
        EventManager.off(MyEvent.RefreshBackpackGoodsItem, this.RefreshPlayerGoodsItem, this);
        EventManager.off(MyEvent.RefreshInventoryButtons, this.RefreshInventoryButtons, this);
    }
}
